//
//  ShopCentMapViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/10/29.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "ShopCentMapViewController.h"

#import "JJAnnotation.h"
#import "MyAnnoModel.h"

@interface ShopCentMapViewController ()<BMKMapViewDelegate>{
    BMKMapView *_mapView;
    BMKLocationService *location;
    JJAnnotation *jjano;
}

@property (nonatomic,strong)NSMutableArray *array;

@property (nonatomic,strong)NSMutableArray *datas;


@end

@implementation ShopCentMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    _datas = [NSMutableArray array];
    _array = [NSMutableArray array];
    [self loadUI];
    //[self loadData];
}

- (void)loadUI{
    _mapView = [[BMKMapView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    _mapView.delegate = self;
    [_mapView setZoomEnabled:YES];
    [_mapView setZoomLevel:14];//设置级别
    [self.view addSubview:_mapView];
    
    //定位服务
    location = [[BMKLocationService alloc] init];
    //开启定位
    [location startUserLocationService];
    
    //显示定位图层
    _mapView.showsUserLocation = YES;
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(30, 30, 28, 28);
    [btn setImage:[UIImage imageNamed:@"箭头3"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    _mapView.delegate = self;
    location.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    _mapView.delegate = nil;
    location.delegate = nil;
}

#pragma -mark BMKLocationServiceDelegate
-(void)didUpdateUserHeading:(BMKUserLocation *)userLocation{
    [_mapView updateLocationData:userLocation];
    _mapView.centerCoordinate = userLocation.location.coordinate;
    //
    
}

-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    [_mapView updateLocationData:userLocation];
    _mapView.centerCoordinate = userLocation.location.coordinate;
    
    [self loadData];
}

//商家地图显示骑手
- (void)loadData{
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:_lng forKey:@"longitude"];
    [parmas setObject:_lat forKey:@"latitude"];
    NSString *url = [NSString stringWithFormat:@"%@/App/Map/merchantMap",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        _array = [responseObject objectForKey:@"result"];
        _datas = [responseObject objectForKey:@"result"];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

//加载商家
- (void)loadAnno{
    for (NSDictionary *dic in _array) {
        MyAnnoModel *model = [[MyAnnoModel alloc] initWithAnnotationModelWithDict:dic];
        
        
        [_datas addObject:model];
        
    }
    [_mapView addAnnotations:_datas];
}

- (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
{
    if ([annotation isKindOfClass:[JJAnnotation class]]) {
        
        BMKAnnotationView *annoView = [[BMKAnnotationView alloc] init];
        annoView.image = [UIImage imageNamed:@""];
        
        annoView.canShowCallout = YES;
        return annoView;
        
    }
    
    jjano = (JJAnnotation *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"otherAnnotationView"];
    if (!jjano) {
        jjano = [[JJAnnotation alloc] initWithAnnotation:annotation reuseIdentifier:@"otherAnnotationView"];
    }
    
    jjano.image = [UIImage imageNamed:@"店未选中"];
    jjano.imageView.image = [UIImage imageNamed:@"店未选中"];
    
    MyAnnoModel *model = (MyAnnoModel *)annotation;
    
    jjano.dic = model.dic;
    NSString *index = [model.dic objectForKey:@"id"];
    
    jjano.tag = [index integerValue];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
    jjano.userInteractionEnabled = YES;
    [jjano addGestureRecognizer:tap];
    
    return jjano;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
